package com.shaoshaohuo.app.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.CarTypeSecondListAdapter;
import com.shaoshaohuo.app.constant.CarOption;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.db.DaoUtils;
import com.shaoshaohuo.app.entity.CarTypeFirst;
import com.shaoshaohuo.app.entity.CarTypeSecond;
import com.shaoshaohuo.app.entity.PoiEntity;
import com.shaoshaohuo.app.entity.SearchCar;
import com.shaoshaohuo.app.utils.PoiEntityUtils;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.citymenu.CascadingMenuPopWindow;
import com.shaoshaohuo.app.view.citymenu.entity.Area;
import com.shaoshaohuo.app.view.citymenu.interfaces.CascadingMenuViewOnSelectListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by cao on 2016/1/12.
 */
public class SearchCarActivity extends BaseActivity implements View.OnClickListener {

    private TopbarView topbar;

    private View layout_shifadi;
    private View layout_mudidi;
    private View layout_car_type;
    private View layout_car_weight;
    private View layout_car_long;
    private View layout_type;
    private TextView textview_shifadi;
    private TextView textview_mudidi;
    private TextView textview_car_type;
    private TextView textview_car_weight;
    private TextView textview_car_long;
    private TextView textview_type;

    private CascadingMenuPopWindow cascadingMenuPopWindowStart;
    private CascadingMenuPopWindow cascadingMenuPopWindowEnd;

    private AlertDialog carTypeDialog;
    private CarTypeSecond carTypeSecond;

    private AlertDialog carWeightDialog;

    private AlertDialog carLongDialog;

    private AlertDialog typeDialog;

    private SearchCar searchCar = new SearchCar();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_car);
        initView();
        setUpView();
        initData();
    }


    private void initView() {
        topbar = (TopbarView) findViewById(R.id.topbar);

        layout_shifadi = findViewById(R.id.layout_shifadi);
        layout_mudidi = findViewById(R.id.layout_mudidi);
        layout_car_type = findViewById(R.id.layout_car_type);
        layout_car_weight = findViewById(R.id.layout_car_weight);
        layout_car_long = findViewById(R.id.layout_car_long);
        layout_type = findViewById(R.id.layout_type);
        textview_shifadi = (TextView) findViewById(R.id.textview_shifadi);
        textview_mudidi = (TextView) findViewById(R.id.textview_mudidi);
        textview_car_type = (TextView) findViewById(R.id.textview_car_type);
        textview_car_weight = (TextView) findViewById(R.id.textview_car_weight);
        textview_car_long = (TextView) findViewById(R.id.textview_car_long);
        textview_type = (TextView) findViewById(R.id.textview_type);
    }

    private void setUpView() {
        topbar.setCenterText(" ");
        topbar.setLeftView(true, true);
        topbar.setRightText("确定");
        topbar.setRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(ExtraName.searchCar, searchCar);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

        layout_shifadi.setOnClickListener(this);
        layout_mudidi.setOnClickListener(this);
        layout_car_type.setOnClickListener(this);
        layout_car_weight.setOnClickListener(this);
        layout_car_long.setOnClickListener(this);
        layout_type.setOnClickListener(this);

    }


    private void initData() {
        Intent intent = getIntent();
        searchCar = (SearchCar) intent.getSerializableExtra(ExtraName.searchCar);
        if (searchCar == null) {
            searchCar = new SearchCar();
        }else{
            if(!TextUtils.isEmpty(searchCar.origincityid))
            textview_shifadi.setText(searchCar.originName);
            if(!TextUtils.isEmpty(searchCar.receivecityid))
            textview_mudidi.setText(searchCar.receiveName);
            if(!TextUtils.isEmpty(searchCar.carcid))
                textview_car_type.setText(searchCar.carName);
            if(!TextUtils.isEmpty(searchCar.weightName))
                textview_car_weight.setText(searchCar.weightName);
            if(!TextUtils.isEmpty(searchCar.longName))
                textview_car_long.setText(searchCar.longName);

                if ("2".equals(searchCar.triptype)) {
                    textview_type.setText("回程车");
                }else{
                    textview_type.setText("直发车");
                }

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_shifadi:
                if (cascadingMenuPopWindowStart == null) {
                    //向三级menu添加地区数据
                    cascadingMenuPopWindowStart = new CascadingMenuPopWindow(this, false);
                    cascadingMenuPopWindowStart
                            .setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {
                                @Override
                                public void getValue(Area city, String fullname) {
                                    textview_shifadi.setText(fullname);
                                    searchCar.origincityid = PoiEntityUtils.convert(city, fullname).getId();
                                    searchCar.originName = fullname;
                                }
                            });
                    cascadingMenuPopWindowStart.showAsDropDown(textview_shifadi, 5, 5);
                } else if (cascadingMenuPopWindowStart != null
                        && cascadingMenuPopWindowStart.isShowing()) {
                    cascadingMenuPopWindowStart.dismiss();
                } else {
                    cascadingMenuPopWindowStart.showAsDropDown(textview_shifadi, 5, 5);
                }
                break;
            case R.id.layout_mudidi:
                if (cascadingMenuPopWindowEnd == null) {
                    //向三级menu添加地区数据
                    cascadingMenuPopWindowEnd = new CascadingMenuPopWindow(this, false);
                    cascadingMenuPopWindowEnd
                            .setMenuViewOnSelectListener(new CascadingMenuViewOnSelectListener() {
                                @Override
                                public void getValue(Area city, String fullname) {
                                    textview_mudidi.setText(fullname);
                                    searchCar.receivecityid = PoiEntityUtils.convert(city, fullname).getId();
                                    searchCar.receiveName = fullname;
                                }
                            });
                    cascadingMenuPopWindowEnd.showAsDropDown(textview_mudidi, 5, 5);
                } else if (cascadingMenuPopWindowEnd != null
                        && cascadingMenuPopWindowEnd.isShowing()) {
                    cascadingMenuPopWindowEnd.dismiss();
                } else {
                    cascadingMenuPopWindowEnd.showAsDropDown(textview_mudidi, 5, 5);
                }
                break;
            case R.id.layout_car_type:
                selectCarType();
                break;
            case R.id.layout_car_weight:
                selectCarWeight();
                break;
            case R.id.layout_car_long:
                selectCarLong();
                break;
            case R.id.layout_type:
                selectType();
                break;
        }
    }

    protected void selectCarType() {
        if (carTypeDialog != null && carTypeDialog.isShowing()) {
            return;
        }
        if (carTypeDialog == null) {
            List<CarTypeFirst> findAllCartype = DaoUtils.findAllCartype();
            final List<CarTypeSecond> carTypeSeconds = new ArrayList<CarTypeSecond>();
            for (CarTypeFirst carType : findAllCartype) {
                List<CarTypeSecond> findAllCartype2 = DaoUtils.findAllCartype(carType.getTypeid());
                carTypeSeconds.addAll(findAllCartype2);
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            CarTypeSecondListAdapter<CarTypeSecond> adapter = new CarTypeSecondListAdapter<CarTypeSecond>(this, carTypeSeconds,
                    false);
            builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    carTypeSecond = carTypeSeconds.get(which);
                    searchCar.cartype = carTypeSecond.getParentId();
                    searchCar.carcid = carTypeSecond.getCid();
                    searchCar.carName = carTypeSecond.getName();
                    textview_car_type.setText(carTypeSecond.getName());
                }
            });
            carTypeDialog = builder.create();
            carTypeDialog.setTitle("车辆类型");

        }
        carTypeDialog.show();
    }

    protected void selectCarWeight() {
        if (carWeightDialog != null && carWeightDialog.isShowing()) {
            return;
        }
        if (carWeightDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(CarOption.carWeightItems, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    searchCar.weightoption = which + 1;
                    searchCar.weightName = CarOption.carWeightItems[which];
                    textview_car_weight.setText(CarOption.carWeightItems[which]);
                }
            });
            carWeightDialog = builder.create();
            carWeightDialog.setTitle("载重");
        }
        carWeightDialog.show();
    }

    protected void selectCarLong() {
        if (carLongDialog != null && carLongDialog.isShowing()) {
            return;
        }
        if (carLongDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(CarOption.carLongItems, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    searchCar.longoption = which + 1;
                    searchCar.longName = CarOption.carLongItems[which];
                    textview_car_long.setText(CarOption.carLongItems[which]);
                }
            });
            carLongDialog = builder.create();
            carLongDialog.setTitle("车长");
        }
        carLongDialog.show();
    }

    private void selectType() {
        if (typeDialog != null && typeDialog.isShowing()) {
            return;
        }
        if (typeDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setItems(new String[]{"直发车", "回程车"}, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            textview_type.setText("直发车");
                            searchCar.triptype = "1";
                            break;
                        case 1:
                            textview_type.setText("回程车");
                            searchCar.triptype = "2";
                            break;
                    }
                }
            });
            typeDialog = builder.create();
            typeDialog.setTitle("车源类型");
        }
        typeDialog.show();
    }

}
